<template>
  <div
    class="com_container"
    style="position: relative;"
  >
    <div
      class="com_eachrt"
      ref="echart"
    >
    </div>

  </div>
</template>

<script>
import { getAction } from '@/api/manage';
import '@/assets/echart/theme/chalk';
export default {
  data() {
    return {
      //echartInstance实例类
      chartInstance: null,
      //数据集合
      dataList: null,
      // 当前显示的数据
      currentIndex: 0,
      // 定时器的标识
      timerId: null,
    };
  },
  mounted() {
    this.initEchart();
    this.getData();
    //设置监听事件，监听窗口大小动态改变echart的大小（注意销毁）
    window.addEventListener('resize', this.screenAdapter);
    // 在页面加载完成的时候, 主动进行屏幕的适配
    this.screenAdapter();
  },
  computed: {},
  methods: {
    //初始化echart
    initEchart() {
      //初始化echart对象，并指明主题
      this.chartInstance = this.$echarts.init(this.$refs.echart, 'chalk');
      const initOption = {
        title: {
          text: '▎库存和销量分析',
          left: 20,
          top: 20,
        },
      };
      this.chartInstance.setOption(initOption);
      this.chartInstance.on('mouseover', () => {
        clearInterval(this.timerId);
      });
      this.chartInstance.on('mouseout', () => {
        this.startInterval();
      });
    },
    //获取服务器数据
    async getData() {
      const { data: resArr } = await getAction('/echart/getJson', {
        fileName: 'stock.json',
      });
      this.dataList = resArr;
      //更新echart
      this.updateChart();
      //启动定时任务
      this.startInterval();
    },
    //更新echart
    updateChart() {
      const centerArr = [
        ['18%', '40%'],
        ['50%', '40%'],
        ['82%', '40%'],
        ['34%', '75%'],
        ['66%', '75%'],
      ];
      const colorArr = [
        ['#4FF778', '#0BA82C'],
        ['#E5DD45', '#E8B11C'],
        ['#E8821C', '#E55445'],
        ['#5052EE', '#AB6EE5'],
        ['#23E5E5', '#2E72BF'],
      ];
      // 处理图表需要的数据
      const start = this.currentIndex * 5;
      const end = (this.currentIndex + 1) * 5;
      const showData = this.dataList.slice(start, end);
      const seriesArr = showData.map((item, index) => {
        return {
          type: 'pie',
          //设置中心点坐标
          center: centerArr[index],
          hoverAnimation: false, // 关闭鼠标移入到饼图时的动画效果
          labelLine: {
            show: false, // 隐藏指示线
          },
          label: {
            position: 'center',
            color: colorArr[index][0],
          },
          data: [
            {
              name: item.name + '\n\n' + item.sales,
              value: item.sales,
              //颜色渐进
              itemStyle: {
                color: new this.$echarts.graphic.LinearGradient(0, 1, 0, 0, [
                  {
                    offset: 0,
                    color: colorArr[index][0],
                  },
                  {
                    offset: 1,
                    color: colorArr[index][1],
                  },
                ]),
              },
            },
            {
              value: item.stock,
              label: {
                show: false,
              },
              itemStyle: {
                color: '#333843',
              },
            },
          ],
        };
      });
      const dataOption = {
        series: seriesArr,
      };
      this.chartInstance.setOption(dataOption);
    },
    //启动定时任务
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId);
      }
      this.timerId = setInterval(() => {
        this.currentIndex++;
        if (this.currentIndex * 5 >= this.dataList.length) {
          this.currentIndex = 0;
        }
        this.updateChart(); // 在更改完currentIndex之后 , 需要更新界面
      }, 3000);
    },
    // 当浏览器的大小发生变化的时候, 会调用的方法, 来完成屏幕的适配
    screenAdapter() {
      // 和分辨率大小相关的配置项
      const titleFontSize = (this.$refs.echart.offsetWidth / 100) * 3.6;
      const innerRadius = titleFontSize * 2.8;
      const outterRadius = innerRadius * 1.125;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        series: [
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2,
            },
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2,
            },
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2,
            },
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2,
            },
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2,
            },
          },
        ],
      };
      this.chartInstance.setOption(adapterOption);
      // 手动的调用图表对象的resize 才能产生效果
      this.chartInstance.resize();
    },
  },
  destroyed() {
    //销毁监听事件
    window.removeEventListener('resize', this.screenAdapter);
    //清除定时任务
    clearInterval(this.timerId);
  },
};
</script>

<style lang="scss" scoped>
</style>